package com.zhsm.fuxi.mvp.ui.activitys

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.hankkin.reading.base.BaseMvpActivity
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.zhsm.fuxi.R
import com.zhsm.fuxi.app.Constant
import com.zhsm.fuxi.bean.OrderInfo
import com.zhsm.fuxi.bean.OrderListResp
import com.zhsm.fuxi.mvp.contract.OrderCenterContract
import com.zhsm.fuxi.mvp.presenter.OrderCenterPresenter
import com.zhsm.fuxi.mvp.ui.adapter.OederCenterRecyclerAdapter
import com.zhsm.paipaiout.baselibrary.widget.view.PageLayout
import kotlinx.android.synthetic.main.activity_order_center.*
import kotlinx.android.synthetic.main.layout_head_bar.view.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * Created by GuZhC on 2018/10/19.
 * describe:
 */
class OrderCenterActivity : BaseMvpActivity<OrderCenterPresenter>(), OrderCenterContract.IView,
        PageLayout.OnRetryClickListener, OnRefreshLoadMoreListener {

    val mAdapter: OederCenterRecyclerAdapter = OederCenterRecyclerAdapter(ArrayList<OrderInfo>())
     val mPageLayout by lazy{
        PageLayout.Builder(this)
                .initPage(findViewById(R.id.orderCenterRecycler))
                .setDefaultEmptyText("暂无订单")
                .setEmptyDrawable(R.mipmap.empty_order)
                .setDefaultErrorText("网络错误，请点击刷新")
                .setErrorDrawable(R.mipmap.error_net)
                .setOnRetryListener(this)
                .create()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_order_center
    }

    override fun registerPresenter() = OrderCenterPresenter::class.java


    override fun initView() {
        setMiuiStatusBar()
        setToolBar()
        orderCenterRecycler.layoutManager = LinearLayoutManager(this)
        orderCenterRecycler.adapter = mAdapter
        orderRefresh.setOnRefreshLoadMoreListener(this)
    }

    private fun setToolBar() {
        orderCenterHeadBar.imgLift.onClick { finish() }
        orderCenterHeadBar.tvTitle.text = "订单"
        orderCenterHeadBar.textRight.text = "开发票"
        orderCenterHeadBar.textRight.visibility = View.VISIBLE
        orderCenterHeadBar.onClick { startActivity(Intent(this@OrderCenterActivity, InvoiceActivity::class.java)) }
    }

    var pager = 1
    override fun initData() {
        super.initData()
        orderRefresh.autoRefresh()
    }

    override fun onRetry() {
        orderRefresh.autoRefresh()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout?) {
        if (haveMore)
            getPresenter().getOrderList(pager)
        else orderRefresh.finishLoadMoreWithNoMoreData()
    }

    override fun onRefresh(refreshLayout: RefreshLayout?) {
        pager = 1
        haveMore = true
        orderRefresh.isEnableLoadMore = true
        orderRefresh.setNoMoreData(false)
        getPresenter().getOrderList(pager)
    }

    var haveMore = true
    override fun getOrderListSucess(it: OrderListResp?) {
        if (it == null) getOrderListFail()
        it?.run {
            if (list!!.isNotEmpty()) {
                if (pager == 1) {
                    if (list[0].state == Constant.ORDER.EXECUTING) {
                        if(list.size>1) list[1].isShowTop = true
                    }
                    else list[0].isShowTop = true
                    mAdapter.replaceData(list)
                    orderRefresh.finishRefresh(0,true)
                } else {
                    mAdapter!!.addData(list)
                    orderRefresh.finishLoadMore(0,true,false)
                }
                if (list?.size == pageSize) pager++
                else haveMore = false
            } else{
                orderRefresh.finishLoadMore(0,true,false)
                orderRefresh.finishRefresh(0, true)
                haveMore = false
            }
            if (mAdapter.data.isEmpty()){
                mPageLayout.showEmpty()
                orderRefresh.isEnableLoadMore = false
            }
            else mPageLayout.hide()
        }
    }

    override fun getOrderListFail() {
        orderRefresh.finishRefresh(0,false)
        orderRefresh.finishLoadMore(0,false,false)
        mPageLayout.showError()
        orderRefresh.isEnableLoadMore = false
    }
}
